package xyf.p04.tree;

import commons.TreeNode;

/**
 * ClassName: P0235_LowestCommonAncestor
 * Description:
 * Author: xyf
 * Date: 9/8/22 6:23 PM
 * Version: 1.0
 **/
public class P0235_LowestCommonAncestor
{
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q)
    {
        while (true)
        {
            if (p.val < root.val && q.val < root.val)
            {
                root = root.left;
            }
            else if (p.val > root.val && q.val > root.val)
            {
                root = root.right;
            }
            else
            {
                break;
            }
        }
        return root;
    }
}